import controlP5.*;
import java.util.Map;



class GraphControls{
  ControlP5 cp5;
  DropdownList dropDownCateg,dropDownSizes;
  GraphContainer gc;
  Button colorButton;
  HashMap<String, categ> cats;
  boolean drawGraph=false;
  
  int posX,posY;
  GraphControls(int posX,int posY,ControlP5 _cp5,HashMap<String, categ> cats)
  {
    this.posX=posX;
    this.posY=posY;
    this.cats=cats;
   cp5= _cp5;
   
  dropDownCateg = cp5.addDropdownList("categDropdownList",posX+10,posY-6,100,120);
  dropDownSizes= cp5.addDropdownList("sizesDropdownList",posX+140,posY-6,100,120);
  colorButton=  cp5.addButton("color")
     .setValue(0)
     .setPosition(posX+270,posY-24)
     .setSize(50,19).setColorBackground(color(255,0,255));
     ;
     
         cp5.addButton("Add")
     .setValue(0)
     .setPosition(posX+330,posY-24)
     .setSize(50,19)
     ;
              cp5.addButton("Hide/Show")
     .setValue(0)
     .setPosition(posX+390,posY-24)
     .setSize(55,19)
     ;

  
  dropDownListCateg(dropDownCateg,cats);
  dropDownListSizes(dropDownSizes);

}
void createGraph(int w,int h,GraphType gType)
{
  println("cr graph",posX,posY+h+20,w,h,gType);
  gc=new GraphContainer(posX,posY+20,w,h,new GraphType(GraphType.POPULARITY));
  
}
void addGraph(String genre,int w,int h,color c,GraphType gType)
{
  drawGraph=true;
  println("add graph gcs",genre,w,h,c,gType);
  if(gc==null)
    createGraph(w,h,gType);
    gc.Resize(w,h);
    if(genre!="All")
    gc.AddGraph(cats.get(genre).films,genre,c);
    else
    {
      ArrayList<film> allFilms=new ArrayList<film>();
    for(categ cat:cats.values())
    allFilms.addAll(cat.films);
     gc.AddGraph(allFilms,genre,c);
      
    
  }
  drawGraph=true;
}
void changeType(GraphType gType)
{
  if(gc!=null)
    gc.gType=gType;
}
void dropDownListCateg(DropdownList ddl,HashMap<String, categ> cats) {
  ddl.setBackgroundColor(color(190));
  ddl.setItemHeight(20);
  ddl.setBarHeight(15);
  ddl.captionLabel().set("genre");
  ddl.captionLabel().style().marginTop = 3;
  ddl.captionLabel().style().marginLeft = 3;
  ddl.valueLabel().style().marginTop = 3;
  int i=0;
  for(String genre:cats.keySet()) {
    ddl.addItem(genre,i++);
  }
  ddl.addItem("All",i++);
  ddl.setColorBackground(color(60));
  ddl.setColorActive(color(255,128));
}

void dropDownListSizes(DropdownList ddl)
{
   ddl.setBackgroundColor(color(190));
  ddl.setItemHeight(20);
  ddl.setBarHeight(15);
  ddl.captionLabel().set("size");
  ddl.captionLabel().style().marginTop = 3;
  ddl.captionLabel().style().marginLeft = 3;
  ddl.valueLabel().style().marginTop = 3;
int i=0;
ddl.addItem("800x600",i++);
ddl.addItem("700x480",i++);
ddl.addItem("400x300",i++);
ddl.addItem("320x180",i++);

  ddl.setColorBackground(color(60));
  ddl.setColorActive(color(255,128));
    
  
}

void addGraphs()
{
}
int  getColorPickerPositionX()
{
  return (int)colorButton.getPosition().x;
}
int  getColorPickerPositionY()
{
  return (int)colorButton.getPosition().y;
}
void move(int posX,int posY)
{
  this.posX=posX;
  this.posY=posY;
  if(gc!=null)
  gc.Move(posX,posY);
  
}

void draw()
{ 
  strokeWeight(4);
  stroke(225);
  fill(color(128));
    rect(posX,posY-30,450,30);
  strokeWeight(1);

    if(drawGraph)
  gc.Draw();
}
void changeColor(color c)
{
colorButton.setColorBackground(c);  

}
void changeGraphType(GraphType ggtype)
{
gc.gType=ggtype; 

}
}
